package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsCommonService;
import org.darkforest.common.services.GoodsService;
import org.darkforest.common.services.StoreBindClassService;
import org.darkforest.dao.mapper.StoreBindClassMapper;
import org.darkforest.model.StoreBindClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Libing on 2015/10/6.
 */
@Service
@Transactional
public class StoreBindClassServiceImpl implements StoreBindClassService {

    @Autowired
    private StoreBindClassMapper storeBindClassMapper;
    @Autowired
    private GoodsCommonService goodsCommonService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 根据条件查询StoreBindClass
     *
     * @param storeBindClass
     * @return
     */
    @Override
    public List<StoreBindClass> selectStoreBindClassList(StoreBindClass storeBindClass) {
        return storeBindClassMapper.selectStoreBindClassList(storeBindClass);
    }

    /**
     * 店铺绑定分类列表分页查询
     *
     * @param storeBindClass
     * @return
     */
    @Override
    public StoreBindClass selectStoreBindClassListByPage(StoreBindClass storeBindClass) {
        storeBindClass.setResultList(storeBindClassMapper.selectStoreBindClassList(storeBindClass));
        return storeBindClass;
    }

    /**
     * 添加商品绑定分类
     *
     * @param storeBindClass
     * @return
     */
    @Override
    public int addStoreBindStoreGoodsClass(StoreBindClass storeBindClass) {
        return storeBindClassMapper.insertSelective(storeBindClass);
    }

    @Override
    public int updateByPrimaryKeySelective(StoreBindClass storeBindClass) {
        return storeBindClassMapper.updateByPrimaryKeySelective(storeBindClass);
    }

    /**
     * 批量删除店铺绑定分类
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteStoreBindClassBatch(String[] ids) {
        List<String> list = new ArrayList<String>();
        for(String id:ids){
            list.add(id);
        }
        return    storeBindClassMapper.deleteStoreBindClassBatch(list);
    }

    /**
     * 下架店铺对应分类的商品
     *
     * @param ids
     * @param storeId
     */
    @Override
    public void soldOutStoreGoods(String[] ids, Integer storeId) {
        List<String> list = new ArrayList<String>();
        for(String id:ids){
            list.add(id);
            StoreBindClass storeBindClass = storeBindClassMapper.selectByPrimaryKey(Integer.parseInt(id));
            Map map  = new HashMap();
            map.put("class1",storeBindClass.getClass1());
            map.put("class2",storeBindClass.getClass2());
            map.put("class3",storeBindClass.getClass3());
            map.put("storeId",storeId);
            goodsCommonService.soldOutStoreGoods(map);
            goodsService.soldOutStoreGoods(map);

        }
        storeBindClassMapper.deleteStoreBindClassBatch(list);
    }
}
